package cn.ecut.controller;
import	java.util.ArrayList;

import checkers.units.quals.A;
import cn.ecut.model.*;
import cn.ecut.service.YuangongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yuangong")
public class yuangongController {
    @Autowired
    private YuangongService yuangongService;

    @ResponseBody
    @RequestMapping("/login")
    public Message checkLogin(HttpSession session, String mima , String yuangongHao){
        Message message = new Message();
        String s = yuangongService.selectMimaByYuangongHao(yuangongHao);
        if(s.equals(mima)){
            session.setAttribute("mima",mima);
            session.setAttribute("yuangongHao",yuangongHao);
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            message.setMsg("用户名或密码错误");
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/changeMima")
    public Message changeMima(HttpSession session, String mima, String mima2){
        System.out.println("进入到了controller");
        Message message = new Message();
        String yuangongHao = (String) session.getAttribute("yuangongHao");
        if (mima.equals(session.getAttribute("mima"))){
            Yuangong yuangong = new Yuangong();
            yuangong.setMima(mima2);
            yuangong.setYuangongHao(yuangongHao);
            Integer role = yuangongService.updateMimaByYuangongHao(yuangong);
            if(role == 1){
                message.setFlag(true);
                return message;
            }else {
                message.setFlag(false);
                message.setMsg("修改密码失败");
                return message;
            }
        }
        message.setFlag(false);
        message.setMsg("原密码有误");
        return message;
     }

     @ResponseBody
     @RequestMapping("/showallxinxi")
    public Yuangong showAllxinxi(HttpSession session){
        Yuangong yuangong = new Yuangong();
        String s = (String) session.getAttribute("yuangongHao");
        yuangong = yuangongService.selectAllByYuangongHao(s);
         System.out.println(yuangong);
        return yuangong;
     }

     @ResponseBody
    @RequestMapping("/updateallxinxi")
    public Message updateAllxinxi(HttpSession session, HttpServletRequest request,Yuangong yuangong) {
         Message message = new Message();
         yuangong.setMima((String) session.getAttribute("mima"));
         Integer role = yuangongService.updateAllByYuangongHao(yuangong);
         if (role == 1) {
             message.setFlag(true);
             return message;
         } else {
             message.setFlag(false);
             message.setMsg("修改失败");
             return message;
         }
     }

     @ResponseBody
    @RequestMapping("/selectAllxiangmu")
    public Message selectAllxiangmu(){
        Message message = new Message();
        List<Xiangmu> list = new ArrayList<> ();
        list=yuangongService.selectAllxiangmu();
        Iterator<Xiangmu> iterator = list.iterator();
        while(iterator.hasNext()){
           Xiangmu xiangmu =iterator.next();
            if(xiangmu.getQiyongzhuangtai() == 0){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
     }

     @ResponseBody
    @RequestMapping("/selectAllloudong")
    public Message selectAllloudong(String xiangmubianma) {
        Message message = new Message();
        List<Loudong> list = new ArrayList<> ();
        list = yuangongService.selectAllloudongByxiangmubianma(xiangmubianma);
        Iterator<Loudong> iterator = list.iterator();
        while (iterator.hasNext()) {
            Loudong loudong = iterator.next();
            if (loudong.getQiyongzhuangtai() == 0){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectAlltaofang")
    public Message selectAlltaofang(String loudongbianma){
        Message message = new Message();
        List<Taofang> list = new ArrayList<>();
        list = yuangongService.selectAlltaofangByloudongbianma(loudongbianma);
        Iterator<Taofang> iterator = list.iterator();
        while (iterator.hasNext()){
            Taofang taofang = iterator.next();
            if(taofang.getQiyongzhuangtai() == 0){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectAllfangjian")
    public Message selectAllfangjian(String taofangbianma){
        Message message = new Message();
        List<Fangjian> list = new ArrayList<>();
        list = yuangongService.selectAllfangjianBytaofangbianma(taofangbianma);
        Iterator<Fangjian> iterator = list.iterator();
        while (iterator.hasNext()){
            Fangjian fangjian = iterator.next();
            if(fangjian.getQiyongzhuangtai() == 0){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectAllpuwei")
    public Message select(String fangjianbianma){
        Message message = new Message();
        List<Puwei> list = new ArrayList<> ();
        list = yuangongService.selectAllpuweiByfangjianbianma(fangjianbianma);
        Iterator<Puwei> iterator = list.iterator();
        while (iterator.hasNext()){
            Puwei puwei = iterator.next();
            if (puwei.getQiyongzhuangtai() == 0 || puwei.getShiyongzhuangtai() != 1){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
    }
}
